home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / gi / overrides / __init__.pyc (.txt) next >
Python Compiled Bytecode  |  2014-12-31  |  8KB  |  180 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. import types
  5. import warnings
  6. from gi import PyGIDeprecationWarning
  7. from gi._gi import CallableInfo
  8. from gi._constants import TYPE_NONE, TYPE_INVALID
  9. from pkgutil import extend_path
  10. __path__ = extend_path(__path__, __name__)
  11. registry = None
  12.  
  13. def wraps(wrapped):
  14.     
  15.     def assign(wrapper):
  16.         wrapper.__name__ = wrapped.__name__
  17.         wrapper.__module__ = wrapped.__module__
  18.         return wrapper
  19.  
  20.     return assign
  21.  
  22.  
  23. class _Registry(dict):
  24.     
  25.     def __setitem__(self, key, value):
  26.         '''We do checks here to make sure only submodules of the override
  27.         module are added.  Key and value should be the same object and come
  28.         from the gi.override module.
  29.  
  30.         We add the override to the dict as "override_module.name".  For instance
  31.         if we were overriding Gtk.Button you would retrive it as such:
  32.         registry[\'Gtk.Button\']
  33.         '''
  34.         if not key == value:
  35.             raise KeyError('You have tried to modify the registry.  This should only be done by the override decorator')
  36.         
  37.         try:
  38.             info = getattr(value, '__info__')
  39.         except AttributeError:
  40.             raise TypeError('Can not override a type %s, which is not in a gobject introspection typelib' % value.__name__)
  41.  
  42.         if not value.__module__.startswith('gi.overrides'):
  43.             raise KeyError('You have tried to modify the registry outside of the overrides module. This is not allowed (%s, %s)' % (value, value.__module__))
  44.         g_type = info.get_g_type()
  45.         if not g_type != TYPE_NONE:
  46.             raise AssertionError
  47.         if None != TYPE_INVALID:
  48.             g_type.pytype = value
  49.             module = value.__module__[13:]
  50.             key = '%s.%s' % (module, value.__name__)
  51.             super(_Registry, self).__setitem__(key, value)
  52.  
  53.     
  54.     def register(self, override_class):
  55.         self[override_class] = override_class
  56.  
  57.  
  58.  
  59. class overridefunc(object):
  60.     '''decorator for overriding a function'''
  61.     
  62.     def __init__(self, func):
  63.         if not isinstance(func, CallableInfo):
  64.             raise TypeError('func must be a gi function, got %s' % func)
  65.         modules = modules
  66.         import importer
  67.         module_name = func.__module__.rsplit('.', 1)[-1]
  68.         self.module = modules[module_name]._introspection_module
  69.  
  70.     
  71.     def __call__(self, func):
  72.         setattr(self.module, func.__name__, func)
  73.         return func
  74.  
  75.  
  76. registry = _Registry()
  77.  
  78. def override(type_):
  79.     '''Decorator for registering an override'''
  80.     if isinstance(type_, (types.FunctionType, CallableInfo)):
  81.         return overridefunc(type_)
  82.     None.register(type_)
  83.     return type_
  84.  
  85.  
  86. def deprecated(fn, replacement):
  87.     '''Decorator for marking methods and classes as deprecated'''
  88.     
  89.     def wrapped(*args, **kwargs):
  90.         warnings.warn('%s is deprecated; use %s instead' % (fn.__name__, replacement), PyGIDeprecationWarning, stacklevel = 2)
  91.         return fn(*args, **kwargs)
  92.  
  93.     wrapped = (None, wraps(fn))(wrapped)
  94.     return wrapped
  95.  
  96.  
  97. def deprecated_init(super_init_func, arg_names, ignore = tuple(), deprecated_aliases = { }, deprecated_defaults = { }, category = PyGIDeprecationWarning, stacklevel = 2):
  98.     '''Wrapper for deprecating GObject based __init__ methods which specify
  99.     defaults already available or non-standard defaults.
  100.  
  101.     :param callable super_init_func:
  102.         Initializer to wrap.
  103.     :param list arg_names:
  104.         Ordered argument name list.
  105.     :param list ignore:
  106.         List of argument names to ignore when calling the wrapped function.
  107.         This is useful for function which take a non-standard keyword that is munged elsewhere.
  108.     :param dict deprecated_aliases:
  109.         Dictionary mapping a keyword alias to the actual g_object_newv keyword.
  110.     :param dict deprecated_defaults:
  111.         Dictionary of non-standard defaults that will be used when the
  112.         keyword is not explicitly passed.
  113.     :param Exception category:
  114.         Exception category of the error.
  115.     :param int stacklevel:
  116.         Stack level for the deprecation passed on to warnings.warn
  117.     :returns: Wrapped version of ``super_init_func`` which gives a deprecation
  118.         warning when non-keyword args or aliases are used.
  119.     :rtype: callable
  120.     '''
  121.     
  122.     def new_init(self, *args, **kwargs):
  123.         '''Initializer for a GObject based classes with support for property
  124.         sets through the use of explicit keyword arguments.
  125.         '''
  126.         if args:
  127.             warnings.warn('Using positional arguments with the GObject constructor has been deprecated. Please specify keyword(s) for "%s" or use a class specific constructor. See: https://wiki.gnome.org/PyGObject/InitializerDeprecations' % ', '.join(arg_names[:len(args)]), category, stacklevel = stacklevel)
  128.             new_kwargs = dict(zip(arg_names, args))
  129.         else:
  130.             new_kwargs = { }
  131.         new_kwargs.update(kwargs)
  132.         aliases_used = []
  133.         for key, alias in deprecated_aliases.items():
  134.             if alias in new_kwargs:
  135.                 new_kwargs[key] = new_kwargs.pop(alias)
  136.                 aliases_used.append(key)
  137.                 continue
  138.         if aliases_used:
  139.             warnings.warn('The keyword(s) "%s" have been deprecated in favor of "%s" respectively. See: https://wiki.gnome.org/PyGObject/InitializerDeprecations' % ((', '.join,)((lambda .0: pass)(sorted(aliases_used))), ', '.join(sorted(aliases_used))), category, stacklevel = stacklevel)
  140.         defaults_used = []
  141.         for key, value in deprecated_defaults.items():
  142.             if key not in new_kwargs:
  143.                 new_kwargs[key] = deprecated_defaults[key]
  144.                 defaults_used.append(key)
  145.                 continue
  146.         if defaults_used:
  147.             warnings.warn('Initializer is relying on deprecated non-standard defaults. Please update to explicitly use: %s See: https://wiki.gnome.org/PyGObject/InitializerDeprecations' % (', '.join,)((lambda .0: pass)(sorted(defaults_used))), category, stacklevel = stacklevel)
  148.         for key in ignore:
  149.             if key in new_kwargs:
  150.                 new_kwargs.pop(key)
  151.                 continue
  152.         return super_init_func(self, **new_kwargs)
  153.  
  154.     return new_init
  155.  
  156.  
  157. def strip_boolean_result(method, exc_type = None, exc_str = None, fail_ret = None):
  158.     '''Translate method\'s return value for stripping off success flag.
  159.  
  160.     There are a lot of methods which return a "success" boolean and have
  161.     several out arguments. Translate such a method to return the out arguments
  162.     on success and None on failure.
  163.     '''
  164.     
  165.     def wrapped(*args, **kwargs):
  166.         ret = method(*args, **kwargs)
  167.         if ret[0]:
  168.             if len(ret) == 2:
  169.                 return ret[1]
  170.             return None[1:]
  171.         if exc_type:
  172.             if not exc_str:
  173.                 pass
  174.             raise exc_type('call failed')
  175.         return fail_ret
  176.  
  177.     wrapped = (None, None, None, wraps(method))(wrapped)
  178.     return wrapped
  179.  
  180.